class AuthTagClass{
    readonly None = 0;
    readonly Read = 1;
    readonly Write = 2;
    readonly Add = 4;
    readonly Delete = 8;
    readonly Lock = 128;
    readonly Archive = 256;
    readonly All = 0xFFFF;
    readonly(authTag:number|undefined){
        return !this.writable(authTag)
    }
    /** 只有明确授权可写权限，才返回true。  */
    writable(authTag:number|undefined){

        if(authTag){
            return (authTag & this.Write) == this.Write;
        }else{
            return false;
        }
    }
    /** 根据authTag判断是否可锁定,只有明确授权为Lock，才返回true */
    lockable(authTag:number|undefined){
       if(authTag){
           return (authTag & this.Lock) == this.Lock;
       }else{
           return false;
       }
    }
    /** 根据authTag判断是否可归档 */
    archivable(authTag:number|undefined){
        if(authTag){
            return (authTag & this.Archive) == this.Archive;
        }else{
            return false;
        }
    }
}
let AuthTag : AuthTagClass = new AuthTagClass();



let DataLockState={
    UNLOCKED:0,
    LOCKED:1,
    ARCHIVED:5,
    /** 非锁定状态 */
    unlocked:function(lockState:number|undefined){
        if(lockState){
            return lockState == this.UNLOCKED;
        }else{
            return true;
        }
    },
     /** 锁定或者归档状态 */
     locked:function(lockState:number|undefined){
        if(lockState){
            return lockState == this.LOCKED || lockState == this.ARCHIVED;
        }else{
            return true;
        }
    },
     /** 归档状态 */
     archived:function(lockState:number|undefined){
        if(lockState){
            return lockState == this.ARCHIVED;
        }else{
            return true;
        }
    }
}

class ActionState{
    useAuth:boolean = false;
    useLock:boolean = false;
    authTag:number = 0xFFFF;
    lockState:number = 0;
    masterLockState:number = 0;
    constructor(useAuth:boolean, useLock:boolean, authTag:number, lockState:number, masterLockState:number){
        this.useAuth = useAuth;
        this.useLock = useLock;
        this.authTag = authTag;
        this.lockState = lockState;
        this.masterLockState = masterLockState;
    }

    setActionState(authTag:number, lockState:number, masterLockState:number){
        if(authTag == null || authTag == undefined) authTag = 0xFFFF;
        if(lockState == null || lockState == undefined) lockState = 0;
        if(masterLockState == null || masterLockState == undefined) masterLockState = 0;
        this.authTag = authTag;
        this.lockState = lockState;
        this.masterLockState = masterLockState;
    }

    readonly(){
        let writeable = true;
        if(this.useAuth){//受权限控制
            if(AuthTag.readonly(this.authTag)){//只有只读权限
                writeable = false;
            }else{//有写权限
                if(this.masterLockState == DataLockState.LOCKED || this.masterLockState == DataLockState.ARCHIVED){//但是主数据已经锁定或者已经归档
                    writeable = false;
                }
                if(this.lockState == DataLockState.LOCKED || this.lockState == DataLockState.ARCHIVED){//但是数据已经锁定或者已经归档
                    writeable = false;
                }
            }
        }else{//不受权限控制
            if(this.masterLockState == DataLockState.LOCKED || this.masterLockState == DataLockState.ARCHIVED){//但是主数据已经锁定或者已经归档
                writeable = false;
            }
            if(this.lockState == DataLockState.LOCKED || this.lockState == DataLockState.ARCHIVED){//但是数据已经锁定或者已经归档
                writeable = false;
            }
        }
        return !writeable;
    }

    writeable(){
        return !this.readonly();
    }
}

export {ActionState,AuthTag, DataLockState }